package com.tjec.project.portal.service.impl;

import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.portal.domain.BmsMsg;
import com.tjec.project.portal.mapper.BmsMsgMapper;
import com.tjec.project.portal.service.IBmsMsgService;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 系统消息Service业务层处理
 *
 * @author liuxu
 * @date 2020-05-07
 */
@Service
public class BmsMsgServiceImpl implements IBmsMsgService {
    @Autowired
    private BmsMsgMapper bmsMsgMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService sysUserService;
    @Value("${EC.guids}")
    private String guids;
    /**
     * 查询系统消息
     *
     * @param guid 系统消息ID
     * @return 系统消息
     */
    @Override
    public BmsMsg selectBmsMsgByGuid(String guid) {
        return bmsMsgMapper.selectBmsMsgByGuid(guid);
    }

    /**
     * 查询系统消息列表
     *
     * @param bmsMsg 系统消息
     * @return 系统消息
     */
    @Override
    public List<BmsMsg> selectBmsMsgList(BmsMsg bmsMsg) {
        return bmsMsgMapper.selectBmsMsgList(bmsMsg);
    }

    /**
     * 新增系统消息
     *
     * @param bmsMsg 系统消息
     * @return 结果
     */
    @Override
    public int insertBmsMsg(BmsMsg bmsMsg) {
        return bmsMsgMapper.insertBmsMsg(bmsMsg);
    }

    @Override
    public int insertByReceiverUser(String receiverUserId, String msgType, String content,Boolean type) {
        BmsMsg bmsMsg = new BmsMsg();
        String guid="";
        if(type){
            guid = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        }else{
            //定时任务 取管理员
            guid=guids;
        }
        SysUser sysUser = sysUserService.selectUserById(guid);
        bmsMsg.setSendUserId(guid);
        //bmsMsg.setSendUserName(StringUtils.isNotBlank(sysUser.getNickName()) ? sysUser.getNickName() : sysUser.getUserName());
        bmsMsg.setUserType(sysUser.getUserType());

        bmsMsg.setReceiverUserId(receiverUserId);
        bmsMsg.setMsgType(msgType);
        bmsMsg.setContent(content);
        bmsMsg.setSendTime(new Date());
        return bmsMsgMapper.insertBmsMsg(bmsMsg);
    }

    /**
     * 修改系统消息
     *
     * @param bmsMsg 系统消息
     * @return 结果
     */
    @Override
    public int updateBmsMsg(BmsMsg bmsMsg) {
        return bmsMsgMapper.updateBmsMsg(bmsMsg);
    }

    /**
     * 一键已读
     *
     * @param bmsMsg 系统消息
     * @return 结果
     */
    @Override
    public int batchRead(BmsMsg bmsMsg) {
        return bmsMsgMapper.batchRead(bmsMsg);
    }

    /**
     * 批量删除系统消息
     *
     * @param guids 需要删除的系统消息ID
     * @return 结果
     */
    @Override
    public int deleteBmsMsgByGuids(String[] guids) {
        return bmsMsgMapper.deleteBmsMsgByGuids(guids);
    }

    /**
     * 删除系统消息信息
     *
     * @param guid 系统消息ID
     * @return 结果
     */
    @Override
    public int deleteBmsMsgByGuid(String guid) {
        return bmsMsgMapper.deleteBmsMsgByGuid(guid);
    }
}
